<?php
class RencanaPanenModel{
	private $db;
	private $subbagian;
	
	private $year;
	
	private $chart_file;
	private $chart_type;
	private $chart_value;

	private $validColumn;
	private $data;
	
	function RencanaPanenModel($dbObject){
		$this->subbagian 	= 'rencana_panen';		
		$this->db 			= $dbObject;
		
		// init param
		$this->chart_type  = $this->getGET('chart_type', 'column-column');
		$this->year 	   = $this->getGET('year', date('Y', time()));
		
		$this->validColumn  = array(
			'correction_factor', 
			);
	}
	
	function format($data){
		$result = array();
		foreach($this->validColumn as $v){
			if( isset($data[$v]) )
				$result[$v] = $data[$v];
			else
				$result[$v] = NULL;
		}
		
		return $result;
	}

	private function insertRencanaPanen(){
        // delete all
        $q = "DELETE FROM `caramel_managerial`.`taksasi_config` WHERE `name`='correction_factor_panen'";
        $this->db->query($q);
        
        // insert new one
        $q = "INSERT INTO `caramel_managerial`.`taksasi_config` (`name`, `value`) VALUES ('correction_factor_panen','".$this->data['correction_factor']."')";
        $this->db->query($q);
	}
	
	public function listYear(){
		$q = 'SELECT DISTINCT YEAR(`tgl_panen`) as tahun FROM `caramel_operational`.`panen_betina` ORDER BY tahun ASC';
		
		return $this->db->get_results($q, ARRAY_A);
	}
	
    public function getCorrectionFactor(){
        $q = "SELECT value FROM `caramel_managerial`.`taksasi_config` WHERE `name`='correction_factor_panen'";
        
        return $this->db->get_var($q);
    }
    
	private function getGET($param, $defaut=NULL){
		if( isset( $_GET[$param] ) && $_GET[$param] != '' )
			return $_GET[$param];
		else
			return $defaut;
	}
	
	public function put($data){
		$this->data	= $this->format($data);// buang field2 yang tidak dibutuhkan
		$this->insertRencanaPanen();	
	}
	
	public function generateChart(){
		// fetch the data
		$this->search();
		
		// write xml setting
		$this->createMainSettingXml();
		$this->createDrillSettingXml();
		
		// write xml data
		$this->createMainDataXml();
		$this->createDrillDataXml();
	}
	
	private function createMainSettingXml(){
		$tpl_path = 'include/chart_template/';
		$tpl_file = 'rencana_panen/setting.xml.php';
		$out_path = 'include/chart_setting/';
		$out_file = 'rencana_panen.xml';
		
		$tpl = new Template($tpl_path);
		
		list($chart_type_rencana, $chart_type_realisasi) = split('-', $this->chart_type);
		
		$contents = array(
			'chart_type_rencana'	=> $chart_type_rencana,
			'chart_type_realisasi'	=> $chart_type_realisasi,
			'chart_title'			=> 'Taksasi Panen, Tahun: '.$this->year,
		);
		
		$tpl->set_vars($contents);
		file_put_contents($out_path.$out_file, $tpl->fetch($tpl_file));
	}
	
    private function createMainDataXml(){ // phplot to amplot chart data type
		$tpl_path = 'include/chart_template/';
		$tpl_file = 'rencana_panen/data.xml.php';
		$out_path = 'include/chart_data/';
		$out_file = 'rencana_panen.xml';
		
		$tpl = new Template($tpl_path);
		
		foreach($this->chart_value as $c){
			$rencana_value[]   = $c[2];
			$realisasi_value[] = $c[1];
		}
		
		$contents = array(
			'rencana_graph_value'	=> $rencana_value,
			'realisasi_graph_value'	=> $realisasi_value,
		);
		$tpl->set_vars($contents);
		file_put_contents($out_path.$out_file, $tpl->fetch($tpl_file));
	}
    
	private function createDrillSettingXml(){
		$tpl_path = 'include/chart_template/';
		$tpl_file = 'rencana_panen/drill_setting.xml.php';
		$out_path = 'include/chart_setting/';
		$out_file = 'rencana_panen_drill.xml';
		
		$tpl = new Template($tpl_path);
		
		$contents = array(
			'chart_type'	=> 'line',
			'chart_title'	=> 'Realisasi Bulan Tertentu',
		);
		$tpl->set_vars($contents);
		file_put_contents($out_path.$out_file, $tpl->fetch($tpl_file));
	}
	
	private function createDrillDataXml(){
		$tpl_path = 'include/chart_template/';
		$tpl_file = 'rencana_panen/drill_data.xml.php';
		$out_path = 'include/chart_data/';
		$out_file = 'rencana_panen_drill.xml';
		
		$tpl = new Template($tpl_path);
		
		$contents = array(
			'message'		=> 'Klik salah satu nilai bulan untuk melihat detailnya disini.',
			'graph_series'	=> array_fill(1,31,0),
			'graph_value'	=> array_fill(1,31,0),
		);
		$tpl->set_vars($contents);
		file_put_contents($out_path.$out_file, $tpl->fetch($tpl_file));
	}
	
    private function calculateTaksasi(){
        $base       = new TaksasiCalculator(); 
        $base_value = $base->calculate();
        
        $dist = new MonthDistributionDAO(); 
        $dist->setTahun($this->year);
        $dist_value = $dist->get();
        
        $config       = new TaksasiConfigDAO(); 
        $config_value = $config->getCorrectionFactorConfig('panen');
        
        $taksasi = new TaksasiBase();
        $taksasi->setBase($base_value);
        $taksasi->setCorrectionFactor($config_value);
        $taksasi->setMonthDistribution($dist_value);

        $panen = $taksasi->calculate();
        
        return $panen;
    }
    
	private function search(){		 
        // nilai aktual
        $query = "
            SELECT MONTH(tgl_panen) as bulan, COUNT(*) as realisasi 
            FROM `caramel_operational`.`panen_betina`
            WHERE YEAR(`tgl_panen`)='".$this->year."'
            GROUP BY MONTH(tgl_panen)
        ";
		
        $result     = $this->db->get_results($query, ARRAY_A);
        
        $realisasi  = array();
        foreach($result as $i=>$value){
            $realisasi[$value['bulan']] = $value['realisasi'];
        }
        
        // taksasi
        $taksasi = $this->calculateTaksasi();
        
        $month	= array(
			"", "Januari","Februari","Maret","April","Mei","Juni",
			"Juli","Agustus","September","Oktober","November","Desember");
	
		$data = array();
		for($i=1; $i<count($month); $i++){
			$data[$i] = array($month[$i], $realisasi[$i], $taksasi[$i]);
		}
        
		$this->chart_value  = $data;
	}
	
	public function getChartValue(){
		return $this->chart_value;
	}

}
?>